package com.ftg.mall.base.util.tooltips;

import lombok.Data;

import java.util.Objects;

/**
 * 前后台信息交互类
 * @author 高铭泽
 * @createTime 2022/11/14
 */

@Data
@SuppressWarnings("all")
public class ResBean<T> {

    Integer code;

    String msg;

    T data;

    public ResBean(Integer code,String msg){
        this.code = code;
        this.msg = msg;
        this.data = null;
    }

    public ResBean(Integer code,String msg,T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ResBean(ResposeEnum resEnum){
        this.code = resEnum.getCode();
        this.msg = resEnum.getMsg();
        this.data = null;
    }

    public ResBean(ResposeEnum resEnum, T data){
        this.code = resEnum.getCode();
        this.msg = resEnum.getMsg();
        this.data = data;
    }

    /**
     * 数据不存在，一般用到删除，修改，添加时前置验证
     * @return
     */
    public static ResBean noData(){
        return new ResBean(ResposeEnum.NO_DATA);
    }

    /**
     * 添加  修改   删除 时出错错误
     * @return
     */
    public static ResBean FAIL_DML(){
        return new ResBean(ResposeEnum.FAIL_DML);
    }

    /**
     * 通用的成功操作封装，api成功以后向前台返回信息调用此方法
     * @return
     */
    public static ResBean success(){
        return new ResBean(ResposeEnum.SUCCESS);
    }

    /**
     * 操作成功以后，带参数向前返回信息
     * @param data
     * @return
     */
    public static ResBean success(Object data){
        if(Objects.isNull(data)){
            return new ResBean(ResposeEnum.SUCCESS_NO_DATA);
        }
        return new ResBean(ResposeEnum.SUCCESS,data);
    }

    /**
     * 通用的成功操作封装，api成功以后向前台返回信息调用此方法
     * @return
     */
    public static ResBean failure(){
        return new ResBean(ResposeEnum.FAIL_EXCEPTION);
    }

    /**
     * 操作成功以后，带参数向前返回信息
     * @param data
     * @return
     */
    public static ResBean failure(Object data){
        return new ResBean(ResposeEnum.FAIL_EXCEPTION,data);
    }

    /**
     * 没有找到地址处理
     * @return
     */
    public static ResBean notFound404(){
        return new ResBean(ResposeEnum.NOT_FOUND_404);
    }


    /**
     * 请求参数不匹配
     * @return
     */
    public static ResBean failInvalidParam(){
        return new ResBean(ResposeEnum.FAIL_INVALID_PARAM);
    }

    /**
     * 请求参数不匹配
     * @return
     */
    public static ResBean failInvalidToken(){
        return new ResBean(ResposeEnum.FAIL_INVALID_TOKEN);
    }

    /**
     * Token无效或已过期
     * @return
     */
    public static ResBean failNoPermission(){
        return new ResBean(ResposeEnum.FAIL_NO_PERMISSION);
    }

    /**
     * 请求资源不存在
     * @return
     */
    public static ResBean failNotFound(){
        return new ResBean(ResposeEnum.FAIL_NOT_FOUND);
    }

    /**
     * 数据校验不通过
     * @return
     */
    public static ResBean failValidation(){
        return new ResBean(ResposeEnum.FAIL_VALIDATION);
    }

    /**
     * 操作执行失败
     * @return
     */
    public static ResBean failOperation(){
        return new ResBean(ResposeEnum.FAIL_OPERATION);
    }

    /**
     * 请求连接超时
     * @return
     */
    public static ResBean failRequestTimeout(){
        return new ResBean(ResposeEnum.FAIL_REQUEST_TIMEOUT);
    }

    /**
     * 认证不通过（用户名密码错误等认证失败场景）
     * @return
     */
    public static ResBean failAuthentication(){
        return new ResBean(ResposeEnum.FAIL_AUTHENTICATION);
    }

    /**
     * 无效的账号
     * @return
     */
    public static ResBean failInvalidAccount(){
        return new ResBean(ResposeEnum.FAIL_INVALID_ACCOUNT);
    }

    /**
     * 系统异常
     * @return
     */
    public static ResBean failException(){
        return new ResBean(ResposeEnum.FAIL_EXCEPTION);
    }

    /**
     * 服务不可用
     * @return
     */
    public static ResBean failServiceUnavailable(){
        return new ResBean(ResposeEnum.FAIL_SERVICE_UNAVAILABLE);
    }

    /**
     * 部分成功（一般用于批量处理场景，只处理筛选后的合法数据）
     * @return
     */
    public static ResBean warnPartialSuccess(){
        return new ResBean(ResposeEnum.WARN_PARTIAL_SUCCESS);
    }

    /**
     * 操作成功，但是数据库里面没有数据，一般用来于查询没有返回数据的情况
     * @return
     */
    public static ResBean successNoData(){
        return new ResBean(ResposeEnum.SUCCESS_NO_DATA);
    }
}
